package com.study.mysql.readwriteseparation1.route;

import com.study.mysql.readwriteseparation1.constant.ClientDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;


@Slf4j
public class ClientDataSourceRouter extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        ClientDataSource clientDataSource = ClientDataSourceContextHolder.getClientDatabase();
        if (clientDataSource == null) {
            log.debug("null client database, use default {}", ClientDataSource.MASTER);
            clientDataSource = ClientDataSource.MASTER;
        }
        log.trace("use {} as database", clientDataSource);
        return clientDataSource;
    }
}
